﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
namespace BYManageInfoSystem
{
    public partial class Frm_FeeBG_TGDFee : DockContent
    {
        private DGBY.BLL.BG_TGDFeeTab m_bllBG_TGDFeeTab = new DGBY.BLL.BG_TGDFeeTab();
        private DGBY.BLL.FactoryGoodsTab m_bllFactoryGoodsTab = new DGBY.BLL.FactoryGoodsTab();
        private DGBY.BLL.BGHarborTab m_bllBGHarborTab = new DGBY.BLL.BGHarborTab();

        private bool m_bInsertFlag = false;
        private int m_nDelIndex = -1;

        public Frm_FeeBG_TGDFee()
        {
            InitializeComponent();
        }

        private void ShowAllRecord(string strWhere)
        {
            List<int> lstID = new List<int>();
            DataSet ds2 = m_bllBG_TGDFeeTab.GetConflictList();
            for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
            {
                int n = (int)ds2.Tables[0].Rows[i][0];
                if (!lstID.Contains(n))
                    lstID.Add(n);
                n = (int)ds2.Tables[0].Rows[i][1];
                if (!lstID.Contains(n))
                    lstID.Add(n);

            }

            DataSet ds = m_bllBG_TGDFeeTab.GetList(strWhere);
            ultraGrid1.DataSource = null;

            DataSet dsTemp = m_bllFactoryGoodsTab.GetList("");

            this.ultraDropDownFac.DataSource = dsTemp.Tables[0];
            this.ultraDropDownFac.DisplayMember = "货品名称";
            this.ultraDropDownFac.ValueMember = "FactoryGoodsTabID";
            this.ultraDropDownFac.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownFac.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownFac.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownFac.DisplayLayout.Bands[0].Columns["货品名称"].Hidden = false;


            dsTemp = m_bllBGHarborTab.GetList("");

            this.ultraDropDownBGPort.DataSource = dsTemp.Tables[0];
            this.ultraDropDownBGPort.DisplayMember = "报关港";
            this.ultraDropDownBGPort.ValueMember = "BGHarborTabID";
            this.ultraDropDownBGPort.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownBGPort.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownBGPort.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownBGPort.DisplayLayout.Bands[0].Columns["报关港"].Hidden = false;


            ultraGrid1.DataSource = ds.Tables[0];
            ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["货品名称"].ValueList = this.ultraDropDownFac;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["报关港"].ValueList = this.ultraDropDownBGPort;


            for (int i = 0; i < ultraGrid1.Rows.Count; ++i)
            {
                UltraGridRow CurrentUGR = ultraGrid1.Rows[i];
                if (lstID.Contains(Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString())))
                    CurrentUGR.Appearance.BackColor = Color.Yellow;
            }
        }
        private void Frm_FeeBG_BGFee_Load(object sender, EventArgs e)
        {
            ShowAllRecord("");
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            //ClassUltraGridCommonSet.LocalPrintPreveiw();
        }

        private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.Control)
            {

                if (!m_bInsertFlag)
                {
                    UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
                    //  row.Cells[2] = DateTime.Parse("1900-01-01");
                }
            }
            switch (e.KeyCode)
            {

                case Keys.Up:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;     

                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;
                        if (strTemp == "货品名称" || strTemp == "报关港")
                            return;
                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;     

                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;
                        if (strTemp == "货品名称" || strTemp == "报关港")
                            return;
                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex < ultraGrid1.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }

        }
        private bool AddGoodsDetail(int nRowIndex)
        {

            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];

                DGBY.Model.BG_TGDFeeTab modelBG_TGDFeeTab = new DGBY.Model.BG_TGDFeeTab();
                modelBG_TGDFeeTab.SPFeePrice = Convert.ToDecimal(CurrentUGR.Cells["应付费用定价"].Value.ToString());
                modelBG_TGDFeeTab.SRFeePrice = Convert.ToDecimal(CurrentUGR.Cells["应收费用定价"].Value.ToString());


                modelBG_TGDFeeTab.FactoryGoodsTabID = Convert.ToInt32(CurrentUGR.Cells["货品名称"].Value.ToString(), 10);
                modelBG_TGDFeeTab.BGHarborTabID = Convert.ToInt32(CurrentUGR.Cells["报关港"].Value.ToString(), 10);
                modelBG_TGDFeeTab.ValidDate = (DateTime)CurrentUGR.Cells["生效日期起始"].Value;
                modelBG_TGDFeeTab.ValidDateEnd = (DateTime)CurrentUGR.Cells["生效日期终止"].Value;

                /**********************************************************************************/
                //2012-09-04
                int nID = m_bllBG_TGDFeeTab.Add(modelBG_TGDFeeTab);

                //ShowAllRecord("");
                Frm_Main.Log2DB(this.Text, "增加", "");
                ShowAllRecord("BG_TGDFeeTab.BG_TGDFeeTabID = '" + nID.ToString() + "'");
                /**********************************************************************************/

                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        private void InitNewAddRowValue(int nIndex)
        {
            UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

            CurrentUGR.Cells["货品名称"].Value = 24;
            CurrentUGR.Cells["报关港"].Value = 10;
            CurrentUGR.Cells["应收费用定价"].Value = 0;

            CurrentUGR.Cells["应付费用定价"].Value = 0;

            CurrentUGR.Cells["生效日期起始"].Value = System.DateTime.Parse("2011-01-01");
            CurrentUGR.Cells["生效日期终止"].Value = System.DateTime.Parse("2199-01-01");
            if (m_bInsertFlag)
            {
                ultraGrid1.Rows[nIndex].Cells[2].Activation = Activation.ActivateOnly;
                m_bInsertFlag = false;
                AddGoodsDetail(nIndex);

            }

        }
        private void ultraGrid1_AfterRowInsert(object sender, RowEventArgs e)
        {
            ultraGrid1.Rows[e.Row.Index].Cells[1].IgnoreRowColActivation = true;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activation = Activation.AllowEdit;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activate();
            ultraGrid1.PerformAction(UltraGridAction.EnterEditMode);
            m_bInsertFlag = true;
            InitNewAddRowValue(e.Row.Index);

        }

        private bool ModifyGoodsDetail(int nRowIndex)
        {

            try
            {

                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];
                int nSelectBG_TGDFeeTabID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                DGBY.Model.BG_TGDFeeTab modelBG_TGDFeeTab = m_bllBG_TGDFeeTab.GetModel(nSelectBG_TGDFeeTabID);
                modelBG_TGDFeeTab.SPFeePrice = Convert.ToDecimal(CurrentUGR.Cells["应付费用定价"].Value.ToString());
                modelBG_TGDFeeTab.SRFeePrice = Convert.ToDecimal(CurrentUGR.Cells["应收费用定价"].Value.ToString());

                string strBGH = CurrentUGR.Cells["货品名称"].Value.ToString();
                if (ClassInputValid.IsNum(strBGH))
                {
                    modelBG_TGDFeeTab.FactoryGoodsTabID = Convert.ToInt32(CurrentUGR.Cells["货品名称"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllFactoryGoodsTab.GetList("FactoryGoodsName = '" + strBGH + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelBG_TGDFeeTab.FactoryGoodsTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["FactoryGoodsTabID"].ToString(), 10);
                    }
                }

                strBGH = CurrentUGR.Cells["报关港"].Value.ToString();
                if (ClassInputValid.IsNum(strBGH))
                {
                    modelBG_TGDFeeTab.BGHarborTabID = Convert.ToInt32(CurrentUGR.Cells["报关港"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllBGHarborTab.GetList("BGHarborName = '" + strBGH + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelBG_TGDFeeTab.BGHarborTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["BGHarborTabID"].ToString(), 10);
                    }
                }

                bool bModifyTime = false;
                DateTime dtValidDate = (DateTime)CurrentUGR.Cells["生效日期起始"].Value;
                DateTime dtValidDateEnd = (DateTime)CurrentUGR.Cells["生效日期终止"].Value;

                if (modelBG_TGDFeeTab.ValidDate != dtValidDate || modelBG_TGDFeeTab.ValidDateEnd != dtValidDateEnd)
                    bModifyTime = true;

                modelBG_TGDFeeTab.ValidDate = dtValidDate;
                modelBG_TGDFeeTab.ValidDateEnd = dtValidDateEnd;
               
                m_bllBG_TGDFeeTab.Update(modelBG_TGDFeeTab);

                DataSet dsTemp = m_bllBG_TGDFeeTab.GetList(
    "BG_TGDFeeTab.BGHarborTabID = '" + modelBG_TGDFeeTab.BGHarborTabID.ToString() + "'"
+ " and BG_TGDFeeTab.FactoryGoodsTabID = '" + modelBG_TGDFeeTab.FactoryGoodsTabID.ToString() + "'"
+ " and ValidDateEnd = '21990101'"
+ " and ValidDate < '" + modelBG_TGDFeeTab.ValidDate + "'");
                if (dsTemp.Tables.Count > 0 && dsTemp.Tables[0].Rows.Count > 0)
                {
                    int nBG_TGDFeeTabID = Convert.ToInt32(dsTemp.Tables[0].Rows[0]["BG_TGDFeeTabID"].ToString(), 10);
                    DGBY.Model.BG_TGDFeeTab modelBG_TGDFeeTabOld = m_bllBG_TGDFeeTab.GetModel(nBG_TGDFeeTabID);
                    modelBG_TGDFeeTabOld.ValidDateEnd = modelBG_TGDFeeTab.ValidDate.Date.AddDays(-1);
                    m_bllBG_TGDFeeTab.Update(modelBG_TGDFeeTabOld);
                }

                Frm_Main.Log2DB(this.Text, "修改", CurrentUGR.Cells["货品名称"].Text);

                if (bModifyTime)
                    ShowAllRecord("");

                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void ultraGrid1_BeforeRowUpdate(object sender, CancelableRowEventArgs e)
        {
            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[e.Row.Index];

                if (m_bInsertFlag)
                {
                    ultraGrid1.Rows[e.Row.Index].Cells[2].Activation = Activation.ActivateOnly;
                    m_bInsertFlag = false;
                    if (!AddGoodsDetail(e.Row.Index))
                        e.Cancel = true;



                }
                else//修改
                {

                    ModifyGoodsDetail(e.Row.Index);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

        private void ultraGrid1_BeforeRowsDeleted(object sender, BeforeRowsDeletedEventArgs e)
        {
            e.DisplayPromptMsg = false;
            if (!m_bInsertFlag)
            {
                int nSelectBG_TGDFeeTabID = Convert.ToInt32(ultraGrid1.ActiveRow.Cells[0].Value.ToString(), 10);
                DGBY.Model.BG_TGDFeeTab modelBG_TGDFeeTab = m_bllBG_TGDFeeTab.GetModel(nSelectBG_TGDFeeTabID);

                if (modelBG_TGDFeeTab == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    try
                    {

                        m_bllBG_TGDFeeTab.Delete(nSelectBG_TGDFeeTabID);

                        Frm_Main.Log2DB(this.Text, "删除", ultraGrid1.ActiveRow.Cells["货品名称"].Text);
                        ShowAllRecord("");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        e.Cancel = true;
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                MessageBox.Show("插入未完成，不能删除记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            m_nDelIndex = ultraGrid1.ActiveRow.Index;

        }

        private void ultraGrid1_AfterRowCancelUpdate(object sender, RowEventArgs e)
        {
            m_bInsertFlag = false;
        }

        private void ultraGrid1_BeforeCellCancelUpdate(object sender, CancelableCellEventArgs e)
        {
            m_bInsertFlag = false;
        }

        private void toolStripMenuItemAdd_Click(object sender, EventArgs e)
        {
            if (!m_bInsertFlag)
            {
                UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
                //  row.Cells[2] = DateTime.Parse("1900-01-01");
            }
        }

        private void toolStripMenuItemDel_Click(object sender, EventArgs e)
        {
            int nIndex = m_nDelIndex;
            if (m_nDelIndex >= 0)
            {
                DialogResult dr = MessageBox.Show("确实要删除该记录么!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.Cancel)
                {
                    return;
                }

                if (ultraGrid1.ActiveRow != null)
                {
                    UltraGridRow CurrentUGR;
                    CurrentUGR = ultraGrid1.Rows[nIndex];
                    ultraGrid1.ActiveRow.Delete(false);
                }
            }
            else
            {
                MessageBox.Show("请选中要删除的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecord("");
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void ultraDropDownFac_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {

        }

        private void ultraGridExcelExporter1_BeginExport(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.BeginExportEventArgs e)
        {

        }

        private void Frm_FeeBG_TGDFee_Load(object sender, EventArgs e)
        {

        }



    }
}
